//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package ai.moran.demo.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;

public class DateTimeUtils {
    public static final String FORMAT_ALL_FIELDS_COMPACT = "yyyyMMddHHmmss";
    public static final String FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String ZONE_ID_BEIJING = "Asia/Shanghai";
    public static final String ES_DEFAULT_FORMAT = "yyyy-MM-dd:HH:mm:ss";
    public static final String HOUR_MINUTE = "HH:mm";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    public DateTimeUtils() {
    }

    public static Date nowDate() {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("Asia/Shanghai"));
        return Date.from(localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant());
    }

    public static String getTimeString(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(Calendar.getInstance().getTime());
    }

    public static Date parse(String dateString, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateString);
    }

    public static String getTimeString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String getTimeString(Date date, String format, TimeZone timeZone) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(timeZone);
        return sdf.format(date);
    }

    public static String getTimeString(int field, int amount, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(field, amount);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(calendar.getTime());
    }

    public static Date getTime(int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(field, amount);
        return calendar.getTime();
    }

    public static Date getTimeByHourMinute(int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(11, hour);
        calendar.set(12, minute);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getDayFromMidNight(int amount) {
        LocalTime midNight = LocalTime.MIDNIGHT;
        LocalDate today = LocalDate.now(ZoneId.of("Asia/Shanghai"));
        LocalDateTime todayMidNight = LocalDateTime.of(today, midNight);
        return Date.from(todayMidNight.plusDays((long)amount).atZone(ZoneId.of("Asia/Shanghai")).toInstant());
    }

    public static Date getDayBasedHour(Date date, int amount) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("Asia/Shanghai"));
        return Date.from(localDateTime.plusHours((long)amount).atZone(ZoneId.of("Asia/Shanghai")).toInstant());
    }

    public static String getTimestamp10Bits() {
        Long bits10 = Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai"))).getTimeInMillis() / 1000L;
        return bits10.toString();
    }

    public static String toFormat(String dateTimeString, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        try {
            return simpleDateFormat.format(simpleDateFormat.parse(dateTimeString));
        } catch (ParseException var4) {
            return "";
        }
    }

    public static String convertFormat(String sourceTimeString, String sourceFormat, String targetFormat) throws Exception {
        SimpleDateFormat sourceSDF = new SimpleDateFormat(sourceFormat);
        SimpleDateFormat targetSDF = new SimpleDateFormat(targetFormat);
        return targetSDF.format(sourceSDF.parse(sourceTimeString));
    }

    public static boolean compare_date(String date1, String date2, String format) throws Exception {
        DateFormat df = new SimpleDateFormat(format);
        Date dt1 = df.parse(date1);
        Date dt2 = df.parse(date2);
        if (dt1.getTime() > dt2.getTime()) {
            return true;
        } else {
            return dt1.getTime() >= dt2.getTime();
        }
    }

    public static Date getStartOfDay(int dayOfMonth, String zoneId) {
        LocalDate localDate = LocalDate.now(ZoneId.of(zoneId)).withDayOfMonth(dayOfMonth);
        return Date.from(Instant.from(localDate.atStartOfDay(ZoneId.of(zoneId))));
    }

    public static long toUnixTimestamp(String datetime, String format) throws Exception {
        Date date = parse(datetime, format);
        return date.getTime() / 1000L;
    }

    public static long toUnixTimestamp() throws Exception {
        Date date = new Date();
        return date.getTime() / 1000L;
    }

    public static String getTimeString(Long unixTimestamp, String format) {
        if (unixTimestamp == null) {
            throw new IllegalArgumentException("unix timestamp is null");
        } else if (StringUtils.isEmpty(format)) {
            throw new IllegalArgumentException("format is empty");
        } else {
            Date date = new Date(unixTimestamp * 1000L);
            return getTimeString(date, format);
        }
    }

    public static long dayGaps(Date sourceDay, Date targetDay) {
        if (sourceDay != null && targetDay != null) {
            long diff = sourceDay.getTime() - targetDay.getTime();
            return diff >= 0L ? TimeUnit.MILLISECONDS.toDays(diff) : 0L - TimeUnit.MILLISECONDS.toDays(0L - diff) - 1L;
        } else {
            throw new IllegalArgumentException("sourceDay/targetDay must not be null");
        }
    }
}
